const state = {
    // 是否存在未读消息
    isReadMsg: true,

    // 未读消息列表（只展示10条）
    msgList: [],

    // 消息总数
    msgTotalCount: 0,

    // 标签列表
    tagList: [{ name: "Preview", path: "/", fatherPath: "", title: "看板", active: true, noCached: true }],

    // 当关闭标签时，下一个选中标签的值
    nextTag: null,

    // 更多下拉列表
    moreList: [],

    // 页面缓存
    // 当页面在标签页中打开，且keep-alive为true时，缓存当前页面的name
    // 当页面标签关闭时，则需要删除此页面的name，这样就不会缓存页面
    cachedViews: [],
}

const mutations = {
    // 设置未读消息
    setMsgList(state, data) {
        state.msgList = data
    },

    // 设置消息总数
    setMsgTotalCount(state, data) {
        state.msgTotalCount = data
    },

    // 重置标签
    resetTags(state, data) {
        state.tagList = data
    },

    // 选择标签
    selectTag(state, name) {
        let tagList = state.tagList

        tagList.forEach(item => {
            item.active = false

            if (item.name === name) {
                item.active = true
            }
        })

        state.tagList = tagList
    },

    // 设置标签
    setTagList(state, row) {
        let tagList = state.tagList
        let currentTag = null

        if (row) {
            tagList.forEach((item) => {
                item.active = false
            })
            
            currentTag = {
                name: row.name,
                path: row.name === "Preview" ? "" : "/" + row.path,
                fatherPath: row.meta.fatherPath,
                title: row.meta.title,
                active: true,
                noCached: false
            }

            tagList = tagList.concat(currentTag)
        }

        state.tagList = tagList
    },

    // 关闭标签功能，删除项：removeTagName，添加项：addTagName
    closeTag(state, { removeTagName, addTagName }) {
        let tagList = state.tagList

        tagList.forEach((item, index) => {
            item.active = false

            if (item.name === addTagName) {
                item.active = true
            }
        })

        tagList.forEach((item, index) => {
            if (item.name === removeTagName) {
                tagList.splice(index, 1)
            }
        })

        state.tagList = tagList
    },

    // 关闭所有标签
    closeAllTags(state) {
        let tagList = state.tagList
        let arr = []

        tagList.forEach((item, index) => {
            if (item.name === "Preview") {
                item.active = true
                arr.push(item)
            }
        })

        state.tagList = arr
    },

    // 设置页面缓存
    setCaches(state, data) {
        state.cachedViews = data
    },

    // 移除页面缓存，names（string or object）：此参数必填，且一定要传 noCached = false 的页面的 name
    removeCache(state, names) {
        let cachedViews = state.cachedViews
        let caches = []

        if (typeof names === "string") {
            caches = cachedViews.filter(item => item !== names)
        } else if (typeof names === "object") {
            cachedViews.forEach((item, index) => {
                names.forEach(j => {
                    if (item === j) {
                        cachedViews.splice(index, names.length)
                    }
                })
            })
        } else {
            console.log("页面缓存清理失败，请检查原因！")
            return
        }

        state.cachedViews = caches
    },

    // 添加更多下拉列表
    setMoreList(state) {
        let data = [
            { name: "关闭所有页面", type: "all" },
            { name: "关闭当前页面", type: "current" },
            { name: "关闭左侧页面", type: "left" },
            { name: "关闭右侧页面", type: "right" },
            { name: "关闭其它页面", type: "other" }
        ]

        state.moreList = data
    }
}

const actions = {
    // 获取未读消息
    getNoReadMsgList(ctx) {
        // type: 0 未读，1 已读
        // msgType: 0 异常订单，1 新订单提醒，2 退款提醒
        // message：0 您有一条系统未读消息，1 来新订单啦，2 有顾客退款
        let data = [
            {
                type: 0,
                msgType: 0,
                message: "单号：XD202252154854685 异常付款，请尽快审核！",
                tips: "您有一条异常订单！",
                createTime: "2023-01-17 14:34:22"
            }, {
                type: 0,
                msgType: 1,
                message: "23 号桌成功下单！",
                tips: "来新订单啦！",
                createTime: "2023-01-17 14:38:22"
            }, {
                type: 0,
                msgType: 2,
                message: "22 号桌顾客退款，单号为：XD265646564465644",
                tips: "有顾客退款，请尽快审核！",
                createTime: "2023-01-17 14:39:15"
            }
        ]

        ctx.commit("setMsgList", data)
        ctx.commit("setMsgTotalCount", 99)
    }
}

const getters = {}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}